<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="presentation/css/index.css">
        <title>Syba</title>
        <script type="text/javascript" src="presentation/scripts_js/fonctions.js"></script>
        <script type="text/javascript" src="presentation/scripts_js/XMLHttpRequest.js"></script>
    </head>
    <body onload="cacher();
            recupererId();">
        <div id="page">
            <header>
                <a href="index.html">
                    <img id="home_direction" src="presentation/images/home.png" alt="Home" />
                </a>
                <div id ="site_title">
                    <h1> SYBA</h1> </div>

                <div id="connexion">
                    <section id="formulaire_edition_eleve">
                        <form method="post" enctype="multipart/form-data" onsubmit="connexion();" action="">
                            <p>
                                <label>Pseudo: <input type="text" id="pseudo" name="pseudo" autocomplete="on" required /></label>
                            </p>
                            <p>
                                <label>Mot de passe : <input type="password" id="mdp" name="mdp" autocomplete="on" required></label>
                            </p>
                            <p>
                                <input type="submit" value ="Se connecter"/>   <a href="presentation/partie_generateur/ens_gestion_adultes_ajout.html" > Créer un compte </a>
                            </p>
                        </form>
                    </section>
                </div>
                <div id="connecté">
                    Bienvenue   <b id="aff_pseudo"></b> <input type="button" id="b_deconnexion" value="deconnexion " onclick="deconnexion();"/>
                </div>

            </header>

            <nav>
                <div id="menu">
                    <ul>
                        <li id="exercices">
                            <div >
                                <p> 
                                    <a href="presentation/partie_lecteur/index.html">
                                        <img src="presentation/images/picture.png" alt="Eleves" />
                                    </a>
                                </p>
                                <p>Jouer</p>
                            </div>
                        </li>
                        <li id ="generateur">
                            <div >
                                <p>
                                    <a href="presentation/generateur.html">
                                        <img src="presentation/images/key.png" alt="Enseignants" />
                                    </a>
                                </p>
                                <p>Administration</p>
                            </div>
                        </li>

                    </ul>
                </div>
                <div style="clear:both;"></div>
            </nav>
            <article>
                <section>

                </section>
            </article>
            <footer>
                <div id="footer">
                    <address> All right reserved ||
                 <a href="mailto:ghada@hotmail.fr">Contacter Admin</a>
                </address>
                </div>
            </footer>
        </div>
        <script type="text/javascript" >
       
        function connexion() {

            var xhr = getXMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                    //alert("[" + xhr.responseText + "]");
                    //callback(xhr.responseText);
                    var docXML = xhr.responseText;
                    if (xhr.responseText === "0") {
                        alert('Votre compte n\'est as encore validé ou\n\
                              L\'indentifiant ou le mot de passe sont invalides');
                    }
                    else {
                        var listeDesEleves = stringToXML(docXML);

                        var liste = listeDesEleves.getElementsByTagName("personne");
                        createCookie("Syba_connexion", liste[0].getElementsByTagName("nom")[0].childNodes[0].nodeValue + " " + liste[0].getElementsByTagName("prenom")[0].childNodes[0].nodeValue, 7);
                        createCookie("Syba_id_personne", liste[0].getElementsByTagName("identifiant")[0].childNodes[0].nodeValue, 7);
                        createCookie("Syba_type_personne", liste[0].getElementsByTagName("type")[0].childNodes[0].nodeValue, 7);


                    }

                }

            };
            var fd = new FormData();
            var p = document.getElementById('pseudo').value;
           
            if (p.indexOf("@") !== -1 ){
                 
                 fd.append ("oui","oui");
                 fd.append ("mail",p);
                 fd.append("mdp", document.getElementById('mdp').value);
                 xhr.open("POST", "presentation/scripts_php/connexion.php", false);
                 xhr.send(fd);
            }
            else {
                
                var elem = document.getElementById('pseudo').value.split('.');
                fd.append ("oui","non");
                fd.append("prenom", elem[0]);
                fd.append("nom", elem[1]);
                fd.append("mdp", document.getElementById('mdp').value);
                xhr.open("POST", "presentation/scripts_php/connexion.php", false);
                xhr.send(fd);
            }
        }


        function cacher()
        {
            if (readCookie("Syba_connexion") !== null) {
                document.getElementById('connexion').setAttribute('style', 'display:none;');
                document.getElementById('aff_pseudo').innerHTML = readCookie("Syba_connexion");
            }
            else {
                document.getElementById('connecté').setAttribute('style', 'display:none;');
                document.getElementById('generateur').setAttribute('style', 'display:none;');
            }

        }

        function deconnexion() {
            eraseCookie("Syba_connexion");
            eraseCookie("Syba_id_personne");
            eraseCookie("Syba_type_personne");
            location.reload();

        }
        function recupererId() {

            if (readCookie('Syba_id_personne') !== null) {
                if (readCookie('Syba_type_personne') === "Adulte" || readCookie('Syba_type_personne') === "Admin") {
                    var xhr = getXMLHttpRequest();
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
                            //alert("[" + xhr.responseText + "]");
                            //callback(xhr.responseText);
                            var docXML = xhr.responseText;
                            var listeDesAdultes = stringToXML(docXML);
                            cacher_pour_Adulte(listeDesAdultes);
                        }
                    };

                    var fd = new FormData();

                    fd.append("identifiant", readCookie('Syba_id_personne'));

                    xhr.open('POST', "presentation/scripts_php/adulte_recuperer_mysql.php", false);
                    xhr.send(fd);
                }
                else {
                    cacher_pour_Eleve();
                }
            }
        }

      /*  function cacher_pour_Adulte(xml)
        {
            var cook = xml.getElementsByTagName("adulte");

            if ((cook[0].getElementsByTagName("estAdmin")[0].childNodes[0].nodeValue) === '1') {
                document.getElementById('exercices').setAttribute('style', 'display:none;');
            }
        } */
        
        function cacher_pour_Eleve(){
            document.getElementById('generateur').setAttribute('style', 'display:none;');
        }





        </script>
    </body>
</html>